package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.contrarywind.adapter.WheelAdapter;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.transport.PublicSourceEntity3;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.util.DateUtils;

import java.util.ArrayList;
import java.util.List;

//时间段
public class TimeRangePopWindow3 extends BasePopWindow {
    private WheelView wheelView1;
    private WheelView wheelView2;
    private WheelView wheelView3;
    private WheelView wheelView4;
    private List<PublicSourceEntity3> item1List;
    private List<PublicSourceEntity3> item2List;
    private List<PublicSourceEntity3> item3List;
    private List<PublicSourceEntity3> item4List;
    private OnTimeCallBack onTimeCallBack;
    private long startHour;
    private long startMinute;
    private long endHour;
    private long endMinute;
    private String time1 = "00时";
    private String time2 = "00分";
    private String time3 = "00时";
    private String time4 = "00分";

    public TimeRangePopWindow3(Context c) {
        super(c,DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        return R.layout.pop_window_time_range3;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        TextView okView = holder.getPop().findViewById(R.id.ok_view);
        TextView resetView = holder.getPop().findViewById(R.id.reset_view);
        resetView.setOnClickListener(view -> dissMiss());

        wheelView1 = holder.getPop().findViewById(R.id.wheelview1);
        wheelView1.setCyclic(false);
        wheelView2 = holder.getPop().findViewById(R.id.wheelview2);
        wheelView2.setCyclic(false);
        wheelView3 = holder.getPop().findViewById(R.id.wheelview3);
        wheelView3.setCyclic(false);
        wheelView4 = holder.getPop().findViewById(R.id.wheelview4);
        wheelView4.setCyclic(false);

        item1List = getHours();
        wheelView1.setAdapter(new SelectAdapter(item1List));
        wheelView1.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                startHour = item1List.get(index).getId();
                time1 = item1List.get(index).getName();
            }
        });

        item2List = getMunites();
        wheelView2.setAdapter(new SelectAdapter(item2List));
        wheelView2.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                startMinute = item2List.get(index).getId();
                time2 = item2List.get(index).getName();
            }
        });

        item3List = getHours();
        wheelView3.setAdapter(new SelectAdapter(item3List));
        wheelView3.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                endHour = item3List.get(index).getId();
                time3 = item3List.get(index).getName();
            }
        });

        item4List = getMunites();
        wheelView4.setAdapter(new SelectAdapter(item4List));
        wheelView4.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                endMinute = item4List.get(index).getId();
                time4 = item4List.get(index).getName();
            }
        });

        okView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String starTime = startHour + ":"+ startMinute;
                String endTime = endHour + ":" + endMinute;

                String startTime1 = time1.substring(0, time1.length() - 1) + ":" + time2.substring(0, time2.length() - 1);
                String startTime2 = time3.substring(0, time3.length() - 1) + ":" + time4.substring(0, time4.length() - 1);

                long t1 = DateUtils.dateTo13Timestamp2(starTime);
                long t2 = DateUtils.dateTo13Timestamp2(endTime);
                if (onTimeCallBack != null) {
                    onTimeCallBack.result(t1, t2, startTime1  + "-" + startTime2);
                }
                dissMiss();
            }
        });
    }

    public class SelectAdapter implements WheelAdapter{

        private List<PublicSourceEntity3> items;

        SelectAdapter(List<PublicSourceEntity3> list){
            items = list;
        }

        @Override
        public int getItemsCount() {
            return items == null? 0 : items.size();
        }

        @Override
        public Object getItem(int index) {
            if (index >= 0 && index < items.size()) {
                return items.get(index).getName();
            }
            return "";
        }

        @Override
        public int indexOf(Object o) {
            return items.indexOf(o);
        }
    }

    private List<PublicSourceEntity3> getHours() {
        List<PublicSourceEntity3> mOptionsItems = new ArrayList<>();
        for (int i = 0; i < 24; i++) {
            if (i > 9) {
                mOptionsItems.add(new PublicSourceEntity3(i+ "时",i));
            } else {
                mOptionsItems.add(new PublicSourceEntity3("0" + i+ "时", i));
            }
        }
        return mOptionsItems;
    }

    private List<PublicSourceEntity3> getMunites() {
        List<PublicSourceEntity3> mOptionsItems = new ArrayList<>();
        for (int i = 0; i < 60; i++) {
            if (i > 9) {
                mOptionsItems.add(new PublicSourceEntity3(i+ "分",i));
            } else {
                mOptionsItems.add(new PublicSourceEntity3("0" + i+ "分", i));
            }

        }
        return mOptionsItems;
    }

    public interface OnTimeCallBack {
        void result(long startTime, long endTime, String content);
    }

    public void setOnTimeCallBack(OnTimeCallBack onTimeCallBack) {
        this.onTimeCallBack = onTimeCallBack;
    }
}
